<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/14
 * Time: 上午10:02
 */

namespace App\Admin\Controllers;


use App\Busi\Traits\CategoryTrait;
use App\Busi\Traits\CompanyTrait;
use App\Busi\Traits\DistTrait;
use App\Http\Controllers\Controller;
use App\Models\Dist;
use App\Models\SecQualifyCategory;
use App\Models\SupQualifyCategory;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Models\StaffFee;
class StaffFeeController extends Controller
{
    use ModelForm,CompanyTrait,DistTrait,CategoryTrait;
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('费用单项管理');
            $content->description('');
            $content->body($this->grid());
        });
    }

    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('新增费用单项管理');
            $content->description('');
            $content->body($this->form());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑费用单项管理');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    protected function grid() {
        return Admin::grid(StaffFee::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->type('费用类别')->display(function ($type){
                if($type == 0){
                    return "人员费用";
                }elseif ($type == 1){
                    return "服务费用";
                }
                return "业绩费用";
            });
            $obj = $this;
            $grid->sub_category_id('所属资质')->display(function ($category_id) use($obj){
                return $obj->getFullCategoryNameByID($category_id);
            });
            $grid->province_id('省')->display(function ($province_id)use($obj){
                return $obj->getDistNamePid($province_id);
            });
            $grid->city_id('市')->display(function ($city_id)use($obj){
                return $obj->getDistNamePid($city_id);
            });
            $grid->level_id("级别")->display(function ($level){
                $name = "";
                switch ($level){
                    case 1 :$name = "一级";break;
                    case 2 :$name = "二级";break;
                    case 3 :$name = "三级";break;
                    case 4 :$name = "A级";break;
                    case 5 :$name = "B级";break;
                    case 6 :$name = "C级";break;
                    case 7 :$name = "甲级";break;
                    case 8 :$name = "乙级";break;
                    case 9 :$name = "丙级";break;
                    case 10 :$name = "无分级";break;
                    default:$name = "无分级";break;
                }
                return $name;
            });
            $grid->fee_name("费用项目分类");
            $grid->fee_alias("费用项目名称");
            $grid->fee_num("费用项目个数");
            $grid->fee_price("费用项目单价(万)");
            $grid->remark("备注");
            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->created_at('创建时间');
            $grid->filter(function ($filter) {
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {

            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
        });
    }

    protected function form($mode='')
    {
        return Admin::form(StaffFee::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->select('type','类型')->options([0=>"人员费用",1=>"服务费用",2=>"业绩费用"]);
            $form->select('sup_category_id',"一级类目")->options(SupQualifyCategory::pluck("category_name","id"))->load('sub_category_id', '/api/admin_subcategory');
            $form->select("sub_category_id","二级类目")->options(function (){
                $sup_category_id = $this->sup_category_id;
                return SecQualifyCategory::where("parent_cate_id",$sup_category_id)->pluck('category_name','id');
            });
            $form->select('province_id',"省")->options(Dist::where("parent_id",-1)->pluck("dist_name","id"))->load('city_id', '/api/city');
            $form->select("city_id","市")->options(function (){
                $province_id = $this->province_id;
                return Dist::where("parent_id",$province_id)->pluck('dist_name','id');
            });
            $form->select("level_id","等级")->options([1=>'一级',2=>"二级",3=>"三级",4=>"A级",5=>"B级",6=>"C级",7=>"甲级",8=>"乙级",9=>"丙级",10=>"无等级"]);
            $form->text('fee_name','费用项目分类');
            $form->text('fee_alias','费用项目名称');
            $form->text('fee_num','费用项目个数');
            $form->text('fee_price','费用项目单价(万)');
            $form->text('remark','备注(可空)');
            $form->radio('flag', '是否生效')->options([0=>'否', 1=>'是']);
        });
    }

}